<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Zend_Session_SaveHandler_DbTable - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.session.savehandler.dbtable.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.session.savehandler.dbtable.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.session.global_session_management.html">Global Session Management</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.session.html">Zend_Session</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.soap.html">Zend_Soap</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.session.savehandler.dbtable" class="section"><div class="info"><h1 class="title">Zend_Session_SaveHandler_DbTable</h1></div>
    

    <p class="para">
        The basic setup for <span class="classname">Zend_Session_SaveHandler_DbTable</span> must at least
        have four columns, denoted in the config array or <span class="classname">Zend_Config</span> object:
        primary, which is the primary key and defaults to just the session
        id which by default is a string of length 32;
        modified, which is the unix timestamp of the last modified date;
        lifetime, which is the lifetime of the session
        (<strong class="command">modified + lifetime &gt; time();</strong>);
        and data, which is the serialized data stored in the session
    </p>

    <div class="example" id="zend.session.savehandler.dbtable.basic"><div class="info"><p><b>Example #1 Basic Setup</b></p></div>
            

            <pre class="programlisting brush: SQL">
CREATE TABLE `session` (
  `id` char(32),
  `modified` int,
  `lifetime` int,
  `data` text,
  PRIMARY KEY (`id`)
);
</pre>


        <pre class="programlisting brush: php">
//get your database connection ready
$db = Zend_Db::factory(&#039;Pdo_Mysql&#039;, array(
    &#039;host&#039;        =&gt;&#039;example.com&#039;,
    &#039;username&#039;    =&gt; &#039;dbuser&#039;,
    &#039;password&#039;    =&gt; &#039;******&#039;,
    &#039;dbname&#039;    =&gt; &#039;dbname&#039;
));

//you can either set the Zend_Db_Table default adapter
//or you can pass the db connection straight to the save handler $config
Zend_Db_Table_Abstract::setDefaultAdapter($db);
$config = array(
    &#039;name&#039;           =&gt; &#039;session&#039;,
    &#039;primary&#039;        =&gt; &#039;id&#039;,
    &#039;modifiedColumn&#039; =&gt; &#039;modified&#039;,
    &#039;dataColumn&#039;     =&gt; &#039;data&#039;,
    &#039;lifetimeColumn&#039; =&gt; &#039;lifetime&#039;
);

//create your Zend_Session_SaveHandler_DbTable and
//set the save handler for Zend_Session
Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config));

//start your session!
Zend_Session::start();

//now you can use Zend_Session like any other time
</pre>

    </div>

    <p class="para">
        You can also use Multiple Columns in your primary key for
        <span class="classname">Zend_Session_SaveHandler_DbTable</span>.
    </p>

    <div class="example" id="zend.session.savehandler.dbtable.multi-column-key"><div class="info"><p><b>Example #2 Using a Multi-Column Primary Key</b></p></div>
        

        <pre class="programlisting brush: SQL">
CREATE TABLE `session` (
    `session_id` char(32) NOT NULL,
    `save_path` varchar(32) NOT NULL,
    `name` varchar(32) NOT NULL DEFAULT &#039;&#039;,
    `modified` int,
    `lifetime` int,
    `session_data` text,
    PRIMARY KEY (`Session_ID`, `save_path`, `name`)
);
</pre>


        <pre class="programlisting brush: php">
//setup your DB connection like before
//NOTE: this config is also passed to Zend_Db_Table so anything specific
//to the table can be put in the config as well
$config = array(
    &#039;name&#039;              =&gt; &#039;session&#039;, //table name as per Zend_Db_Table
    &#039;primary&#039;           =&gt; array(
        &#039;session_id&#039;,   //the sessionID given by PHP
        &#039;save_path&#039;,    //session.save_path
        &#039;name&#039;,         //session name
    ),
    &#039;primaryAssignment&#039; =&gt; array(
        //you must tell the save handler which columns you
        //are using as the primary key. ORDER IS IMPORTANT
        &#039;sessionId&#039;, //first column of the primary key is of the sessionID
        &#039;sessionSavePath&#039;, //second column of the primary key is the save path
        &#039;sessionName&#039;, //third column of the primary key is the session name
    ),
    &#039;modifiedColumn&#039;    =&gt; &#039;modified&#039;,     //time the session should expire
    &#039;dataColumn&#039;        =&gt; &#039;session_data&#039;, //serialized data
    &#039;lifetimeColumn&#039;    =&gt; &#039;lifetime&#039;,     //end of life for a specific record
);

//Tell Zend_Session to use your Save Handler
Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config));

//start your session
Zend_Session::start();

//use Zend_Session as normal
</pre>

    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.session.global_session_management.html">Global Session Management</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.session.html">Zend_Session</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.soap.html">Zend_Soap</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.session.html">Zend_Session</a></li>
  <li><a href="zend.session.introduction.html">Introduction</a></li>
  <li><a href="zend.session.basic_usage.html">Basic Usage</a></li>
  <li><a href="zend.session.advanced_usage.html">Advanced Usage</a></li>
  <li><a href="zend.session.global_session_management.html">Global Session Management</a></li>
  <li class="active"><a href="zend.session.savehandler.dbtable.html">Zend_Session_SaveHandler_DbTable</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>